﻿using System;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKCopyModel.QuickModelFrame;

namespace YJKCopyModel.GeothermalPipeQuickModel
{
	[Rebuilder(RebuilderName = "GeothermalRebuilder")]
	internal class GeothermalRebuilder : IRevitModelRebuilder
	{
		public bool TryRebuildRevitModel(Autodesk.Revit.DB.Document inputDoc, RevitModelRequest inputReqeust, out BuildData createdElement)
		{
			createdElement = new BuildData();
			GeothermalModelRequest request = inputReqeust as GeothermalModelRequest;
			try
			{
				int num = 0;
				this.CreateModelCurve(inputDoc, request, ref num);
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		private Group CreateGroup(Autodesk.Revit.DB.Document doc, ElementSet elements, int nNumber)
		{
			bool flag = elements.Size < 1;
			Group result;
			if (flag)
			{
				result = null;
			}
			else
			{
				Group group = RevitVersionFuncs.NewGroup(doc, elements);
				group.GroupType.Name = "地热盘管" + nNumber.ToString();
				Parameter parameter = group.GetParameter(BuiltInParameter.GROUP_LEVEL);
				parameter.Set(doc.ActiveView.GenLevel.Id);
				Parameter parameter2 = group.GetParameter(BuiltInParameter.GROUP_OFFSET_FROM_LEVEL);
				parameter2.Set(0.0);
				result = group;
			}
			return result;
		}

		public void CreateModelCurve(Autodesk.Revit.DB.Document doc, GeothermalModelRequest request, ref int nNum)
		{
			XYZ normal = new XYZ(0.0, 0.0, 1.0);
			XYZ origin = new XYZ(0.0, 0.0, 0.0);
			Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(normal, origin);
			SketchPlane sketchPlane = SketchPlane.Create(doc, plane);
			ElementSet elementSet = new ElementSet();
			foreach (Curve curve in request.LstSupplyCurves)
			{
				ModelCurve modelCurve = doc.Create.NewModelCurve(curve, sketchPlane);
				modelCurve.LineStyle = request.SupplyGraphicsStyle;
				elementSet.Insert(modelCurve);
			}
			foreach (Curve curve2 in request.LstBackCurves)
			{
				ModelCurve modelCurve2 = doc.Create.NewModelCurve(curve2, sketchPlane);
				modelCurve2.LineStyle = request.BackGraphicsStyle;
				elementSet.Insert(modelCurve2);
			}
			this.CreateGroup(doc, elementSet, nNum);
			nNum++;
		}
	}
}
